package com.wangfei.lecture.httpclient4_5;

import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @author wangfei 20210510
 */
public class TestHttp {
    /**
     * HttpClient 5.0 requires Java 1.7 or newer.
     */
    @org.junit.Test
    public void test() {
        // 创建cookie store的本地实例
        CookieStore cookieStore = new BasicCookieStore();
        // 全局请求设置
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();

        // 创建HttpClient上下文
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
        try(CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setDefaultCookieStore(cookieStore).build();) {
            HttpGet httpGet = new HttpGet("http://www.biqu5200.net/0_111/");
            try (CloseableHttpResponse response1 = httpClient.execute(httpGet)) {
                System.out.println("status line: " + response1.getStatusLine().getStatusCode() + ", " + response1.getStatusLine().getReasonPhrase() + " " + response1.getEntity());
                HttpEntity entity1 = response1.getEntity();
                System.out.println("page: " + EntityUtils.toString(entity1));
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//    @org.junit.Test
//    public void testPoolingManager() {
//        PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
//                .setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
//                        .setSslContext(SSLContexts.createSystemDefault())
//                        .setTlsVersions(TLS.V_1_3, TLS.V_1_2)
//                        .build())
//                .setDefaultSocketConfig(SocketConfig.custom()
//                        .setSoTimeout(Timeout.ofSeconds(5))
//                        .build())
//                .setPoolConcurrencyPolicy(PoolConcurrencyPolicy.STRICT)
//                .setConnPoolPolicy(PoolReusePolicy.LIFO)
//                .setConnectionTimeToLive(TimeValue.ofMinutes(1L))
//                .build();
//    }


    public static void main(String[] args) {

    }

    public static String test(String url) {
        // 创建cookie store的本地实例
        CookieStore cookieStore = new BasicCookieStore();
        // 全局请求设置
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build();

        // 创建HttpClient上下文
        HttpClientContext context = HttpClientContext.create();
        context.setCookieStore(cookieStore);
        String data = null;
        try(CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig)
                .setDefaultCookieStore(cookieStore).build();) {
            HttpPost httppost = new HttpPost(url);
            try (CloseableHttpResponse response1 = httpClient.execute(httppost)) {
                System.out.println("status line: " + response1.getStatusLine().getStatusCode() + ", " + response1.getStatusLine().getReasonPhrase() + " " + response1.getEntity());
                HttpEntity entity1 = response1.getEntity();
                data = EntityUtils.toString(entity1);
                // do something useful with the response body
                // and ensure it is fully consumed
                EntityUtils.consume(entity1);
            }
        } catch (IOException e) {
            e.printStackTrace();

        }
        return data;
    }
}
